﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using TFXK.Model;
using TFXK.BLL;
using TFXK.Common;
using log4net;

public partial class admin_AdsManage_Action : BasePage
{
    private static readonly ILog log = LogManager.GetLogger(typeof(admin_AdsManage_Action));
    private static readonly AdsBLL bll = new AdsBLL();

    #region 加载
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                InitAction();
            }
        }
        catch (Exception ex)
        {
            log.Error(ex.Message);
        }

    }
    #endregion

    #region 绑定动作
    private void InitAction()
    {
        if (Request.QueryString["type"] != null)
        {
            string type = Request.QueryString["type"].ToString();
            string title = string.Empty;// 设置标题
            // 判断动作
            switch (type)
            {
                case "add":
                    title = "添加广告";
                    imgPic.ImageUrl = "../../images/nopic.jpg";
                    break;
                case "modify":
                    if (Request.QueryString["id"] != null)
                    {
                        title = "广告修改";
                        int id = int.Parse(Request.QueryString["id"].ToString());
                        InitData(id);
                    }
                    else
                    {
                        Response.Redirect("Default.aspx", false);
                    }
                    break;

            }
            this.Title = title;
            this.hdfAction.Value = type;
        }
        else
        {
            Response.Redirect("Default.aspx", false);
        }
    }
    #endregion

    #region 绑定
    private void InitData(int id)
    {
        Ads model = bll.GetModel(id);
        if (model != null)
        {

            imgPic.ImageUrl = string.IsNullOrEmpty(model.linkImage) ? "../../images/nopic.jpg" : "../../uploads/" + model.linkImage;
            hdfPic.Value = model.linkImage;
            txtAdsTitle.Text = model.title;
            txtLinkUrl.Text = model.linkUrl;
            txtOrderby.Text = model.orderid + "";


        }
    }
    #endregion

    #region 添加/修改
    protected void ibtnAdd_Click(object sender, EventArgs e)
    {
        //验证代码
        try
        {
            Ads model = new Ads();
            model.linkdes =  "";//暂无
            model.linkImage = UpLoadImages();
            model.linkUrl = txtLinkUrl.Text.Trim();
            model.orderid = int.Parse(txtOrderby.Text);
            model.title = txtAdsTitle.Text;
            model.typeid = 0;//默认

            string type = this.hdfAction.Value.Trim();
            // 判断动作
            switch (type)
            {
                case "add":
                 
                    Add(model);
                    break;
                case "modify":
                    model.id = int.Parse(Request.QueryString["id"].ToString());
                    Modify(model);
                    break;

            }
        }
        catch (Exception ex)
        {
            log.Error(ex.Message);
            Msg.Show("网络错误!原因:" + ex.Message);
        }
    }
    #endregion

    #region 添加
    private void Add(Ads model)
    {
        int rows = bll.Add(model);
        if (rows > 0)
        {
            Msg.ShowAndRedirect("添加信息成功!", "Default.aspx");
        }
        else
        {
            Msg.Show("添加信息失败!");
        }
    }
    #endregion

    #region 修改
    private void Modify(Ads model)
    {
        bll.Update(model);
        Msg.ShowAndRedirect("修改信息成功!", "Default.aspx");
    }
    #endregion

    #region 返回
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");
    }
    #endregion


    #region 图片上传
    private string UpLoadImages()
    {
        string imgOldPath = hdfPic.Value;
        try
        {
            string saveDir = "../../uploads/"; //文件保存文件夹
            string serverFile = Server.MapPath(saveDir);
            int Nid = 0;
            foreach (DevExpress.Web.ASPxUploadControl.UploadedFile file in uploadControl.UploadedFiles)
            {
                if (!string.IsNullOrEmpty(file.PostedFile.FileName))
                {
                    if (!string.IsNullOrEmpty(imgOldPath))
                    {
                        try
                        {
                            string fullpath = Server.MapPath("../../uploads/" + imgOldPath);
                            System.IO.File.Delete(fullpath);
                        }
                        catch { }
                    }

                    string imgExt = file.FileName.Substring(file.FileName.IndexOf('.'));
                    string saveName = DateTime.Now.ToString("yyyyMMddhhmmss") + imgExt;

                    file.SaveAs(serverFile + saveName);
                    imgOldPath = saveName;
                }
            }
        }
        catch (Exception ex)
        {
            Msg.Show("网络错误:" + ex.Message);
        }
        return imgOldPath;
    }
    #endregion
}
